var lottery={
	index:-1,	//当前转动到哪个位置，起点位置
	count:0,	//总共有多少个位置
	timer:0,	//setTimeout的ID，用clearTimeout清除
	speed:20,	//初始转动速度
	times:0,	//转动次数
	cycle:50,	//转动基本次数：即至少需要转动多少次再进入抽奖环节
	prize:-1,	//中奖位置
	init:function(id){
		if ($("#"+id).find(".lottery-unit").length>0) {
			$lottery = $("#"+id);
			$units = $lottery.find(".lottery-unit");
			this.obj = $lottery;
			this.count = $units.length;
			$lottery.find(".lottery-unit-"+this.index).addClass("active");
		};
	},
	roll:function(){
		var index = this.index;
		var count = this.count;
		var lottery = this.obj;
		$(lottery).find(".lottery-unit-"+index).removeClass("active");
		index += 1;
		if (index>count-1) {
			index = 0;
		};
		$(lottery).find(".lottery-unit-"+index).addClass("active");
		this.index=index;
		return false;
	},
	stop:function(index){
		this.prize=index;
		return false;
	}
};
var prizeIndex=0;
var prizeNum=0;
function roll(){
	lottery.times += 1;
	lottery.roll();
	if (lottery.times > lottery.cycle+10 && lottery.prize==lottery.index) {
		clearTimeout(lottery.timer);
		lottery.prize=-1;
		lottery.times=0;
		//弹出中奖框
		$("#cd-popup-prize .prize_text").text(switchPrize(prizeNum));
		setTimeout(function(){
			$("#cd-popup-prize").fadeIn(300);
			$("#cd-popup-prize .cd-popup-container").fadeIn(300);
		},1000);
		click=false;
	}else{
		if (lottery.times<lottery.cycle) {
			lottery.speed -= 10;
		}else if(lottery.times==lottery.cycle) {
			var index = Math.random()*(lottery.count)|0;
			lottery.prize = index;		
		}else{
			if (lottery.times > lottery.cycle+10 && ((lottery.prize==0 && lottery.index==7) || lottery.prize==lottery.index+1)) {
				lottery.speed += 110;
			}else{
				lottery.speed += 20;
			}
		}
		if (lottery.speed<20) {
			lottery.speed=20;
		};
		//console.log(lottery.times+'^^^^^^'+lottery.speed+'^^^^^^^'+lottery.prize);
		lottery.timer = setTimeout(roll,lottery.speed);
	}
	lottery.stop(prizeIndex);
	return false;
}
var click=false;
window.onload=function(){
	lottery.init('lottery');
	$("#lottery a").click(function(){
		if (click) {
			return false;
		}else{
			$.ajax({
				type:"get",
				url:_appRoot+"/common/activity/getLuckNumber?t="+new Date().getTime(),
				dataType:"json",
				success: function(data){
					if(data.result==-1){
						$("#cd-popup5").fadeIn(300);
						$("#cd-popup5 .cd-popup-container").fadeIn(300);
					}else if(data.result==1){
						lottery.speed=100;
						prizeNum=parseInt(data.message);
						switchPrize(prizeNum);
						roll();
						click=true;
						return false;
					}else{
						clickautohide(data.message,1);
					}
				}

			});
			
		}
	});
	$("#login_btn").click(function(){
		var username=$("#uname").val();
		var pass=$("#password").val();
		pass=MD5(pass);
		var p=encode64(username+pass);
		$.post("${head.appRoot}/login",{_p:p,ajax:1},function(data){
			if(data.result==0){
				clickautohide("登录成功",2)
				location.reload();
			}else{
				clickautohide("帐号名或密码错误",3);
			}
		},"json");
	});
	$('#cd-popup5').on('click', function(event){
		if( $(event.target).is('.cd-popup-close') || $(event.target).is('#cd-popup5') ) {
			event.preventDefault();
			$(this).fadeOut(300);
			$('#cd-popup5 .cd-popup-container').fadeOut(300);
		}
	});
	$('#cd-popup-prize').on('click', function(event){
		if( $(event.target).is('.cd-popup-close') || $(event.target).is('#cd-popup-prize')||$(event.target).is('.tryRaffle_a') ) {
			event.preventDefault();
			$(this).fadeOut(300);
			$('#cd-popup-prize .cd-popup-container').fadeOut(300);
			setTimeout('$("#cd-popup-prize .prize_text").text("")',300);
		}
	});
};
function switchPrize(prizeNum){
	switch(prizeNum){
		case 0:prizeIndex=6;return "易学网200积分";
		case 10:prizeIndex=1;return "CCNA远程课程99元";
		case 11:prizeIndex=7;return "HCNA远程课程99元";
		case 12:prizeIndex=2;return "OCP远程课程3000元";
		case 13:prizeIndex=4;return "RHCE远程课程3000元";
		case 100:prizeIndex=3;return "炫彩机械键盘";
		case 101:prizeIndex=5;return "无线充电鼠标";
		case 102:prizeIndex=0;return "头戴式耳机";
	}
}


